package com.pine.sunshineelder.eldernurselevel.web.handler;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dto.JSONResponse;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.NurseLevelEntity;
import com.pine.sunshineelder.eldernurselevel.ao.LevelAddAo;
import com.pine.sunshineelder.eldernurselevel.ao.LevelModAo;
import com.pine.sunshineelder.eldernurselevel.service.NurseLevelService;

@RestController
@RequestMapping("/api/")
public class NurseLevelHandler implements StringHelper{
	@Autowired
	NurseLevelService levelService;
	
	@PostMapping("/level")
	public JSONResponse registerLevel(LevelAddAo ao) throws Exception{
		return JSONResponse.ok(levelService.updaterLevel(ao));
	}
	@DeleteMapping("/level/{id}")
	public JSONResponse deleteLevel(@PathVariable String id) throws Exception {
		levelService.deleteLevel(id);
		return JSONResponse.ok();
	}
	@DeleteMapping("onlevel/{id}")
	public JSONResponse onDeleteLevel(@PathVariable String id)throws Exception{
		levelService.onDeleteLevel(id);
		return JSONResponse.ok();
	}
	@PutMapping("/mod/{id}")
	public  JSONResponse modifyLevel(@PathVariable String id,LevelModAo ao) throws Exception{
		Map<String, String>map =new HashMap<String, String>();
		map.put("name", ao.getName());
		map.put("level", ao.getLevel());
		map.put("content",ao.getContent());
		NurseLevelEntity modeifLevel = levelService.modeifLevel(id, map);
		return JSONResponse.ok(modeifLevel);
		
	}
	@GetMapping("/level")
	public JSONResponse listUser(String key, String pageNo, String pageSize) throws Exception {
		Integer pageNoUse = parseInt(pageNo, null);
		Integer pageSizeUse = parseInt(pageSize, null);
		QueryResult result = levelService.queryByKey(key, pageNoUse, pageSizeUse);
		return JSONResponse.ok(result);
	}

}
